#include "TTitleBar.h"
#include <QMouseEvent>
#include <QCursor>
#include <QHBoxLayout>

TTitleBar::TTitleBar(const QString& name, QWidget *parent)
  : QFrame(parent), _pressed(false)
{
  setFocusPolicy(Qt::ClickFocus);
  setObjectName(name);

  QHBoxLayout* horizontalLayout = new QHBoxLayout(this);
  horizontalLayout->setContentsMargins(0, 0, 0, 0);
  horizontalLayout->setSpacing(8);

  _textLabel = new TElidedLabel("", this);
  _textLabel->setObjectName("text");
  _textLabel->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Preferred);
  horizontalLayout->addWidget(_textLabel);
}

TTitleBar::TTitleBar(QWidget *parent)
  : QFrame(parent), _pressed(false)
{
  setFocusPolicy(Qt::ClickFocus);

//  QHBoxLayout* horizontalLayout = new QHBoxLayout(this);
//  horizontalLayout->setContentsMargins(0, 0, 0, 0);
//  horizontalLayout->setSpacing(8);

//  _textLabel = new TElidedLabel("", this);
//  _textLabel->setObjectName("text");
//  _textLabel->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Preferred);
//  horizontalLayout->addWidget(_textLabel);
}

const QString& TTitleBar::getText() const
{
  return _textLabel->content();
}

void TTitleBar::setText(const QString& text)
{
  _textLabel->setContent(text);
}

void TTitleBar::setTextVisible(bool setting)
{
  _textLabel->setVisible(setting);
}

void TTitleBar::addWidget(QWidget* w, Qt::Alignment alignment, bool at_end)
{
  QHBoxLayout* horizontalLayout = qobject_cast<QHBoxLayout*> (layout());
  if (at_end)
    horizontalLayout->insertWidget(horizontalLayout->indexOf(_textLabel) + 1, w, 0, alignment);
  else
    horizontalLayout->insertWidget(0, w, 0, alignment);
  w->setFocusPolicy(Qt::ClickFocus);
}

void TTitleBar::removeWidget(QWidget* w)
{
  layout()->removeWidget(w);
}

void TTitleBar::addSpacerItem(QSpacerItem* item, bool at_end)
{
  QHBoxLayout* horizontalLayout = qobject_cast<QHBoxLayout*> (layout());
  if (at_end)
    horizontalLayout->insertSpacerItem(horizontalLayout->indexOf(_textLabel) + 1, item);
  else
    horizontalLayout->insertSpacerItem(0, item);
}

void TTitleBar::removeSpacerItem(QSpacerItem* item)
{
  layout()->removeItem(item);
}

void TTitleBar::mousePressEvent(QMouseEvent* e)
{
  if (e->button() == Qt::LeftButton) {
    QRect r = contentsRect();
    r.adjust(3, 3, -3, 0);
    if (r.contains(e->pos())) {
      e->accept();
      _pressed = true;
      _pressPosition = e->pos();
      _moveStartPosition = e->globalPos();
      emit beginToMove(e->globalPos());
      return;
    }
  }
  return QFrame::mousePressEvent(e);
}

void TTitleBar::mouseMoveEvent(QMouseEvent* e)
{
  if (!_pressed)
    return QFrame::mouseMoveEvent(e);

  e->accept();
  QPoint cpos = e->globalPos();
  QPoint pressPosition = mapToGlobal(_pressPosition);
  QPoint offset = cpos - _moveStartPosition;
  if (offset.x() <= 0) {
    if (cpos.x() > pressPosition.x())
      offset.setX(0);
  } else {
    if (cpos.x() < pressPosition.x())
      offset.setX(0);
  }
  if (offset.y() <= 0) {
    if (cpos.y() > pressPosition.y())
      offset.setY(0);
  } else {
    if (cpos.y() < pressPosition.y())
      offset.setY(0);
  }
  if (offset.x() != 0 || offset.y() != 0)
    emit moveRequested(cpos, offset);
  _moveStartPosition = cpos;
}

void TTitleBar::mouseReleaseEvent(QMouseEvent* e)
{
  if (_pressed) {
    e->accept();
    _pressed = false;
    emit moveFinished(e->globalPos());
  } else {
    QFrame::mouseReleaseEvent(e);
  }
}

bool TTitleBar::event(QEvent* e)
{
  if (e->type() == QEvent::MouseButtonDblClick) {
    QMouseEvent* me = static_cast<QMouseEvent*> (e);
    if (me->button() == Qt::LeftButton) {
      emit mouseButtonDblClicked();
      return true;
    }
  }
  return QFrame::event(e);
}
